package com.wxtech.heartbeat.server;



import org.apache.log4j.Logger;


import com.wxtech.heartbeat.server.protocol.DecodeResult;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 把数据保存到数据库
 */
public class DataHandler extends ChannelInboundHandlerAdapter {
	private static final Logger logger = Logger.getLogger(DataHandler.class);

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) {
		logger.info("channelRead()");
//		Protocol protocol;
		if (!(msg instanceof DecodeResult)) {
			logger.error(msg.getClass().getName());
			throw new RuntimeException("Unknown object");
		}
			
		
		DecodeResult dr  = (DecodeResult )msg;
		logger.info("protocol:");
		dr.handler(ctx);
		
		
	}

	@Override
	public void channelActive(final ChannelHandlerContext ctx) {

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//		cause.printStackTrace();
		logger.error(cause.getMessage(), cause);
		ctx.close();
	}

	

}